#include "mainwindow.h"
#include "ui_mainwindow.h"
#include <QPixmap>

// http://apis.juhe.cn/simpleWeather/query?city=惠州&key=8e1d7ea7ce58f75aeb17cbf74212da15

MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent), ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    manager = new QNetworkAccessManager(this);
    connect(manager, SIGNAL(finished(QNetworkReply *)), this, SLOT(func(QNetworkReply *)));
}

MainWindow::~MainWindow()
{
    delete ui;
}

void MainWindow::on_pushButton_clicked()
{
    QString urlstr = ui->lineEdit->text();

    QUrl url(urlstr);

    QNetworkRequest request(url);

    manager->get(request);
}

void MainWindow::func(QNetworkReply *reply)
{
    QByteArray buf = reply->readAll();

    qDebug() << "响应数据：" << buf;

    QJsonDocument jsondoc = QJsonDocument::fromJson(buf);
    QJsonObject mainobj = jsondoc.object();
    QJsonObject result = mainobj.value("result").toObject();
    QJsonArray future = result.value("future").toArray();

    for (auto day : future)
    {
        QJsonObject dayobj = day.toObject();
        QString date = dayobj.value("date").toString();
        QString temperature = dayobj.value("temperature").toString();
        QString weather = dayobj.value("weather").toString();
        QString direct = dayobj.value("direct").toString();
        QString weatherStr = QString("date:%1\n"
                                     "temperature:%2\n"
                                     "weather:%3\n"
                                     "direct:%4\n")
                                 .arg(date)
                                 .arg(temperature)
                                 .arg(weather)
                                 .arg(direct);
        ui->textBrowser->append(weatherStr);
    }
}
